//
//  MyBillsViewController.m
//  HTP
//
//  Created by 王鑫 on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "MyBillsViewController.h"
#import "MyBillsTableViewCell.h"
#import "MyBillViewModel.h"

#define LENGHT 10

@interface MyBillsViewController () < UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,retain) UITableView *myBillsTableView;

@property (nonatomic,retain) NSDictionary *billDictionary;

@property (nonatomic,retain) NSMutableArray *billArray;

@property int flag;

@end

@implementation MyBillsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myBillsTableView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addCustomTitleWithTitle:@"我的账单"];
    [self addCustomBackBase];
    [self showGetData];
}

- (UITableView *)myBillsTableView {
    if (!_myBillsTableView) {
        _myBillsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - 64) style:UITableViewStyleGrouped];
        _myBillsTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_zhangdan"]];
        _myBillsTableView.delegate = self;
        _myBillsTableView.dataSource = self;
        _myBillsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myBillsTableView.tableHeaderView = [self getHeaderView];
        //上拉设置
        _myBillsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];
        //下拉设置
        _myBillsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
    }
    
    return _myBillsTableView;
}


//TODO:tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_billArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *idendef = @"Identifier";
    MyBillsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idendef];
    
    if (cell == nil) {
        cell = [[MyBillsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idendef];
    }
    
    [cell setCellData:[_billArray objectAtIndex:indexPath.row]];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(section == 0) return 1;
    return 30;
}

//TODO: 头部view
- (UIView *)getHeaderView {
    UIView *sectionHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    
    UILabel *sectionInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 130, 20)];
    sectionInfoLabel.font = [UIFont systemFontOfSize:13.0f];
    sectionInfoLabel.textColor = RGBCOLOR(109, 109, 109, 1);
    sectionHeadView.backgroundColor = RGBCOLOR(225, 225, 225, 1);
    sectionInfoLabel.text = @"最近30天收支明细";
    [sectionHeadView addSubview:sectionInfoLabel];
    
    return sectionHeadView;
}

//TODO: 上拉加载数据
- (void)footRefresh {
    _flag = 1;
    if (_billArray == nil || _billArray.count == 0) {
        [self getBillDateilList:0 whithLenght:LENGHT];
    } else {
        [self getBillDateilList:[_billArray count] whithLenght:LENGHT];
    }
}

- (void)endFootRefresh {
    switch (self.myBillsTableView.mj_footer.state) {
        case MJRefreshStateRefreshing:
            [self.myBillsTableView.mj_footer endRefreshing];
            break;
        default:
            break;
    }
}

//TODO: 下拉刷新数据
- (void)headRefresh {
    _flag = 2;
    if (_billArray == nil || _billArray.count == 0) {
        [self getBillDateilList:0 whithLenght:LENGHT];
    }else {
        [self getBillDateilList:0 whithLenght:[_billArray count]];
    }
}

- (void)endHeadRefresh {
    switch (self.myBillsTableView.mj_header.state) {
        case MJRefreshStateRefreshing:
            [self.myBillsTableView.mj_header endRefreshing];
            break;
        default:
            break;
    }
}

//TODO: 刚进入时的数据加载
- (void)showGetData{
    _flag = 0;
    if (_billArray == nil || _billArray.count == 0) {
        [self getBillDateilList:0 whithLenght:LENGHT];
        return;
    }
    [self.myBillsTableView.mj_header beginRefreshing];
}
//TODO: 获得账单数据
- (void)getBillDateilList:(NSUInteger )startBill whithLenght:(NSUInteger )lenght {
    MyBillViewModel *myBillVM = [[MyBillViewModel alloc] init];
    [myBillVM setBlockWithReturnBlock:^(id returnValue){
        [self hiddenMbProgress];
        NSArray *array = [MyBillModel mj_objectArrayWithKeyValuesArray:returnValue[INTERFACE_INFO]];
        if ([array count] >= startBill + lenght) {
            _billArray = [NSMutableArray arrayWithArray:[array subarrayWithRange:NSMakeRange(0, startBill + lenght)]];
        }else
            _billArray = [NSMutableArray arrayWithArray:[array subarrayWithRange:NSMakeRange(0, [array count])]];
        if (_flag == 1) {
            [self endFootRefresh];
        }else if (_flag == 2) {
            [self endHeadRefresh];
        }
        [self.myBillsTableView reloadData];
    }WithErrorBlock:^(id errorCode) {
        [self hiddenMbProgress];
    }WithFailureBlock:^{
        [self hiddenMbProgress];
    }];
    [self showMbProgress:@""];
    [myBillVM getBillByuserId:[USERCONTENT getUserId]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
